package com.smedi.ismedi.authoritymanagement.core.port.adapter.restful;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.authoritymanagement.api.dto.request.*;
import com.smedi.ismedi.authoritymanagement.api.dto.response.ElementNodeResponse;
import com.smedi.ismedi.authoritymanagement.api.dto.response.ElementResponse;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteElementFeignApi;
import com.smedi.ismedi.authoritymanagement.core.application.command.*;
import com.smedi.ismedi.authoritymanagement.core.application.query.ElementQueryService;
import com.smedi.ismedi.authoritymanagement.core.application.service.ElementApplicationService;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.Element;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.ElementNode;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-7-2 10:51
 */
@Slf4j
@RestController
@AllArgsConstructor
public class ElementController implements RemoteElementFeignApi {
    private final AdapterConverter adapterConverter;
    private final ElementApplicationService elementApplicationService;
    private final ElementQueryService elementQueryService;

    @Override
    public ResponseData<ElementResponse> createElement(ElementCreateRequest elementCreateRequest) {
        ElementCreateCommand elementCreateCommand = adapterConverter.elementCreateRequestToElementCreateCommand(elementCreateRequest);

        Element element = elementApplicationService.createElement(elementCreateCommand);

        ElementResponse elementResponse = adapterConverter.elementToElementResponse(element);

        return new ResponseData<>().success(elementResponse);
    }

    @Override
    public ResponseData<ElementResponse> editElement(ElementEditRequest elementEditRequest) {
        ElementEditCommand elementEditCommand = adapterConverter.elementEditRequestToElementCreateCommand(elementEditRequest);

        Element element = elementApplicationService.editElement(elementEditCommand);
        ElementResponse elementResponse = adapterConverter.elementToElementResponse(element);

        return new ResponseData<ElementResponse>().success(elementResponse);
    }

    @Override
    public ResponseData<Boolean> editElements(List<ElementEditRequest> elementEditRequests) {
        List<ElementEditCommand> elementEditCommands = adapterConverter.elementEditRequestToElementCreateCommand(elementEditRequests);

        elementApplicationService.editElement(elementEditCommands);

        return new ResponseData<>().success(true);
    }

    @Override
    public ResponseData<Boolean> removeElement(ElementRemoveRequest elementRemoveRequest) {
        ElementRemoveCommand elementRemoveCommand = adapterConverter.elementRemoveRequestToElementRemoveCommand(elementRemoveRequest);

        elementApplicationService.removeElement(elementRemoveCommand);

        return new ResponseData<ElementResponse>().success(true);
    }

    @Override
    public ResponseData<ElementResponse> getElementOne(ElementOneRequest elementOneRequest) {
        ElementOneQuery elementOneQuery = adapterConverter.elementOneRequestToElementOneQuery(elementOneRequest);

        Element Element = elementQueryService.getElementOne(elementOneQuery);
        ElementResponse ElementResponse = adapterConverter.elementToElementResponse(Element);

        return new ResponseData<ElementResponse>().success(ElementResponse);
    }

    @Override
    public ResponseData<List<ElementResponse>> getElementList(ElementListRequest elementListRequest) {
        ElementListQuery elementListQuery = adapterConverter.elementListRequestToElementListQuery(elementListRequest);

        List<Element> elements = elementQueryService.getElementList(elementListQuery);
        List<ElementResponse> ElementResponses = adapterConverter.elementToElementResponse(elements);

        return new ResponseData<ElementResponse>().success(ElementResponses);
    }

    @Override
    public ResponseData<PaginationData<ElementResponse>> getElementPage(ElementPageRequest elementPageRequest) {
        ElementPageQuery elementPageQuery = adapterConverter.elementPageRequestToElementPageQuery(elementPageRequest);

        Page<Element> elements = elementQueryService.getElementPage(elementPageQuery);
        PaginationData<ElementResponse> elementResponses = adapterConverter.elementToElementResponse(elements);

        return new ResponseData<ElementResponse>().success(elementResponses);
    }

    @Override
    public ResponseData<List<ElementNodeResponse>> getTree(ElementTreeRequest elementTreeRequest) {
        ElementTreeCommand elementTreeCommand = adapterConverter.elementTreeRequestToElementTreeCommand(elementTreeRequest);

        List<ElementNode> elementNodes = elementQueryService.getTree(elementTreeCommand);
        List<ElementNodeResponse> elementNodeResponses = adapterConverter.nodeToNodeResponse(elementNodes);

        return new ResponseData<ElementResponse>().success(elementNodeResponses);
    }

    @Override
    public ResponseData<Boolean> saveRoleElementRelationship(RoleElementRelationshipSaveRequest roleElementRelationshipSaveRequest) {
        RoleElementRelationshipSaveCommand roleElementRelationshipSaveCommand = adapterConverter.roleElementRelationshipSaveRequestToRoleElementRelationshipSaveCommand(roleElementRelationshipSaveRequest);

        elementApplicationService.saveRoleElementRelationship(roleElementRelationshipSaveCommand);

        return new ResponseData<ElementResponse>().success(true);
    }
}
